package com.xpec.c4.db.usertype;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.ms.db.usertype.BasicToString;
import com.xpec.c4.game.utility.GUID;

public class MapGUIDLongToString extends BasicToString {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8326558551107689250L;

		
	@SuppressWarnings("unchecked")
	@Override
	protected String assemble(Object obj) {
		if(obj == null)
        	return null;
        StringBuffer strBuf = new StringBuffer();
        Map<GUID,Long> map=(Map<GUID,Long>)obj;
        Set<GUID> keys=map.keySet();
        for(GUID key:keys){
        	strBuf.append(key.toString() +SPLITTER);
        	strBuf.append(String.valueOf(map.get(key))+SPLITTER);
        }
        if(strBuf.length()>0){
        	strBuf.setLength(strBuf.length()-1);
        }
        return strBuf.toString();   
	}

	@Override
	protected Object parse(String value) {
		if(value==null){
			return new HashMap<GUID,Long> ();
		}
    	Map<GUID,Long> map = new HashMap<GUID,Long>(); 
    	if(value.length()<=0){
    		return map;
    	}
        String[] strs = value.split(SPLITTER);   
          
        for (int i = 0; i < strs.length; i+=2) {   
            map.put(GUID.parseUUID(strs[i]), Long.valueOf(strs[i+1]));
        }   
        return map;   
	}

	@Override
	public Class<?> returnedClass() {
		return Map.class;
	}

	@Override
	public int[] sqlTypes() {
		return CLOB_TYPES; 
	}

}
